<?php

/*
 * 成就用户逻辑类
 * @author yangchao

 * @package her
 * @copyright yangchao
 * @version 1.2
 * @code-encode utf-8
 * @data-encode utf-8


if (isset($_GET["test_db"]))
{
include_once($_SERVER["DOCUMENT_ROOT"] . "/config/global_config.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/config/db_config.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/config/user_config.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/base.class.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/db.class.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/mem_cache.class.php");
}

*/

class mod_user_achievement
{
	

	function mod_user_achievement()
	{
	
	}

	//根据用户行为ID取数据
	function get_achievement_is_show_by_user_action_id($user_action_id)
	{
		$db_mod_user_achievement = new db_mod_user_achievement();
		
		return $db_mod_user_achievement->get_achievement_is_show_by_user_action_id($user_action_id);
		
	}
	
	//根据ID取单条数据
	function get_one($achievement_id)
	{
		$db_mod_user_achievement = new db_mod_user_achievement();
		$res = $db_mod_user_achievement->get_one($achievement_id);
		if($res)
		{
			//取对应的产品
			$conf = user_global_config::get_instance();
			$res[0]["product_name"] = $conf->get_product_name_by_id($res[0]["achievement_product_id"]);
			$res[0]["type"] = "achievement"; 
		}
		return $res;
	}
	
	//更新获得成就的用户个数
	function  update_user_achievement_achievement_user_count($achievement_id, $achievement_user_count,$achievement_is_on=0)
	{
		$db_mod_user_achievement = new db_mod_user_achievement();
		return $db_mod_user_achievement->update_achievement_user_count($achievement_id,$achievement_user_count,$achievement_is_on);
	}
	
	
	//取最近的
	function get_lastest_user_achievement_list($site_id)
	{
		
		$db_mod_user_achievement = new db_mod_user_achievement();
		$lastest_info = $db_mod_user_achievement->get_lastest_user_achievement_list(1,10,$site_id);
	
		return $lastest_info[0];
	}
	
	//产品下所有16个成就
	function get_all_user_achievement_list_by_site_id_and_product_id($site_id,$product_id=0)
	{
		
		$db_mod_user_achievement = new db_mod_user_achievement();
		$achievement_info = $db_mod_user_achievement->get_achievement_list(1,1000,$site_id,$product_id);
	
		return $achievement_info;
	}
	//取成就列表
	function get_user_achievement_list($p,$pcount,$site_id,$product_id=0)
	{
		
		$db_mod_user_achievement = new db_mod_user_achievement();
		$res = $db_mod_user_achievement->get_achievement_list($p,$pcount,$site_id,$product_id);
	
		return $res;
	}
	//取成就列表
	function get_user_achievement_list_order_by_achievement_attention_count($p,$pcount,$site_id)
	{
		
		$db_mod_user_achievement = new db_mod_user_achievement();
		$res = $db_mod_user_achievement->get_achievement_list_order_by_attention_count($p,$pcount,$site_id);
	
		return $res;
	}
	
	
	/**
	 * 前台获取成就搜索结果总数
	 */
	function get_search_user_achievement_count($name,$site_id,$product_id=0)
	{
		
		$db_mod_user_achievement = new db_mod_user_achievement();
		$res = $db_mod_user_achievement->get_search_achievement_count($name,$site_id,$product_id);
	
		return $res;
	}
	
	
	/**
	 * 前台获取成就搜索结果总数
	 */
	function get_search_user_achievement_list($name,$p,$pcount,$site_id,$product_id=0)
	{
		
		$db_mod_user_achievement = new db_mod_user_achievement();
		$res = $db_mod_user_achievement->get_search_achievement_list($name,$p,$pcount,$site_id,$product_id);
	
		$conf = user_global_config::get_instance();
		
		foreach($res as $key => $value)
		{
			$prodect_id = $value["achievement_product_id"];
			$prodect_name = $conf->get_product_name_by_id($prodect_id);
			$res[$key]["type"] = "achievement";
			$res[$key]["product_name"] = $prodect_name;
			
		}
		
		return $res;
	}
	
	//更新称号关注次数
	function update_user_achievement_attention_count($achievement_id, $achievement_attention_count)
	{
		
		$db_mod_user_achievement = new db_mod_user_achievement();
		$res = $db_mod_user_achievement->update_achievement_attention_count($achievement_id, $achievement_attention_count);
		
		return $res;
	}
	
	/**
	 * 前台获取成就总数
	 */
	function get_user_achievement_count($site_id)
	{
		
		$db_mod_user_achievement = new db_mod_user_achievement();
		$res = $db_mod_user_achievement->get_achievement_count($site_id);
	
		return $res;
	}
	/**
	 * 后台 - 成就  - 重新计算   对应同一用户行为ID的成就总数   &&  在应同一用户行为ID队列中的顺序   
	 */
	function admin_recount_achievement_action_count_and_action_order($user_action_id)
	{
		$achievement = new db_mod_user_achievement();
		
		//获取对应同一行为的成就列表
		$achievement_list = $achievement->admin_get_user_achievement_list(1,10000,false,$user_action_id,false,false,false,false,false,false);
		//获取对应统一行为的成就总数
		$achievement_count = count($achievement_list);
		/* 按量级重排序 */
		foreach($achievement_list as $i => $val)
		{
			$count[$i] = $val["achievement_behavior_count"]; 
		}
		array_multisort($count, SORT_ASC, $achievement_list);
		/* 重排序结束 */
		foreach($achievement_list as $i => $val)
		{
			$order = $i+1;
			$achievement->admin_update_achievement_action_count_and_action_order($val['achievement_id'],$achievement_count,$order);
		}
	}
	
	/**
	 * 判断在对应同一action_id的成就中是否有同样的数量级
	 * @param $achievement_behavior_count
	 * @param $user_action_id
	 */
	function admin_check_same_achievement_action_count($achievement_behavior_count,$user_action_id,$achievement_id="")
	{
		$achievement = new db_mod_user_achievement();
		//获取对应同一行为的成就列表
		$achievement_list = $achievement->admin_get_user_achievement_list(1,10000,false,$user_action_id,false,false,false,false,false,false);
		
		foreach($achievement_list as $i => $val)
		{
			if($achievement_id != $val["achievement_id"])
			{
			
				if($achievement_behavior_count == $val["achievement_behavior_count"]) return true ; 
		
			}
		}
		return false;
	}

}
?>
